package com.hengpeng.itfints.controller.bankproduct;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.Article;
import com.hengpeng.itfinbase.persist.BankProduct;
import com.hengpeng.itfinbase.persist.BankProduct.ProductType;
import com.hengpeng.itfinbase.persist.BankProductContacts;
import com.hengpeng.itfints.constants.TSConstants;
import com.hengpeng.itfints.controller.BaseController;
import com.hengpeng.itfints.service.ServiceFactory;
import com.hengpeng.itfints.tools.PageUtil;
import com.hengpeng.itfints.vo.MemberSession;

/**
 * <pre>
 * 战略联盟银行产品
 * </pre>
 * @author weiyuan
 * @version 1.0, 2015年8月23日
 */
@Controller
@RequestMapping("/bankProduct")
public class BankProductController extends BaseController
{
	public final static Log logger = LogFactory.getLog(BankProductController.class);

	@Autowired
	private ServiceFactory serviceFactory;
	/**
	 * 战略联盟首页
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index")
	public String index(ModelMap map, HttpServletRequest request) {
		List<Article> zxgg = serviceFactory.getArticleService().findArticleListByGroupId(1l, 1);//最新公告
		List<Article> lczx = serviceFactory.getArticleService().findArticleListByGroupId(300l, 5);//理财资讯
		
		List<BankProduct> bankList = serviceFactory.getBankProductService().findBankProductListByType(BankProduct.ProductType.BANK, 5);//银行理财
		List<BankProduct> financeList = serviceFactory.getBankProductService().findBankProductListByType(BankProduct.ProductType.FINAICE, 5);//基金理财
		
		map.put("zxgg", zxgg);
		map.put("lczx", lczx);
		map.put("bankList", bankList);
		map.put("financeList", financeList);
		return "/page/warunion/index.jsp";
	}

	/**
	 * <pre>
	 * 战略联盟银行产品列表
	 * </pre>
	 * @param map
	 * @param request
	 * @param borrowName
	 * @param borrowUserName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/productlist")
	public String list(ModelMap map, HttpServletRequest request,String curPage,ProductType productType)
	{
		int int_curPage = (!StringHelper.isEmpty(curPage) ? Integer.parseInt(curPage) : 1);
		ServiceResult<List<BankProduct>> serviceResult = serviceFactory.getBankProductService().queryList(int_curPage,10,productType);
		PageUtil pUtil = new PageUtil(10, serviceResult.getReturnListSize(), int_curPage);
		map.put("dataList", serviceResult.getReturnValue());
		map.put("PageUtil", pUtil);
		return "/page/warunion/warunionlist.jsp";
	}
	/**
	 * 详细
	 * @param map
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/get")
	public String get(ModelMap map,Long id)
	{
		BankProduct obj = serviceFactory.getPersistService().get(BankProduct.class, id);
		map.put("product", obj);
		return "/page/warunion/waruniondetail.jsp";
	}
	/**
	 * 转向认购意向
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/tobuy", method = RequestMethod.GET)
	public String tobuy(HttpServletRequest request,  ModelMap map, String bankProductId) {
		MemberSession member = (MemberSession) request.getSession().getAttribute(TSConstants.CLIENT_SESSION_MEMBER);
		if (member == null)
		{
			return "/page/borrowInfo/needLogin.jsp";//转到登录页面
		}
		map.put("bankProductId", bankProductId);
		return "/page/warunion/buy.jsp";
	}
	/**
	 * 添加认购意向
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/buy", method = RequestMethod.POST)
	public String buy(HttpServletRequest request, ModelMap map, BankProductContacts bankProductContacts) {
		bankProductContacts.setCreateTime(new Date());
		bankProductContacts.setUpdateTime(new Date());
		bankProductContacts = serviceFactory.getPersistService().save(bankProductContacts);
		if (bankProductContacts.getId() != null) {
			map.put("info", "添加成功");
		} else {
			map.put("info", "添加失败");
		}
		return "/bankProduct/index.htm";
	}
}
